package day15.atm;

import day9.atm.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 存款界面
 */
public class SavePanel extends JPanel implements ActionListener {
    private ATMFrame atmFrame;
    private ImageIcon saveImg = new ImageIcon("images/buttonSure.jpg");
    private JLabel saveLabel = new JLabel("存款金额：");
    private JTextField saveTextField = new JTextField();
    private Font font = new Font("黑体",Font.BOLD,30);
    private JButton saveBtn = new JButton(saveImg);
    public SavePanel(ATMFrame atmFrame){
        this.atmFrame = atmFrame;
        this.setLayout(null);
        saveLabel.setBounds(300,200,180,50);
        saveLabel.setFont(font);

        saveTextField.setBounds(440,200,180,50);
        saveTextField.setFont(font);

        saveBtn.setBounds(442,260,140,50);
        saveBtn.addActionListener(this);
        this.add(saveLabel);
        this.add(saveTextField);
        this.add(saveBtn);
        this.setOpaque(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == saveBtn){
            User user= atmFrame.getLoginUser();
            try {
                user.setBalance(user.getBalance() + Double.parseDouble(saveTextField.getText()));
            }catch (NumberFormatException e1){
                JOptionPane.showMessageDialog(this,"必须输入数字");
                return;
            }
            atmFrame.switchPanel("menu");
        }
    }
}
